package ch.subgate.spritestudio.types;

import java.util.ArrayList;

import android.graphics.Bitmap;

public class PaintEvent {
	private boolean mApplied;
	
	private ArrayList<Pixel> mAffectedPixels;
	
	public PaintEvent() {
		mAffectedPixels = new ArrayList<Pixel>();
		mApplied = false;
	}
	
	public void addPixel(Pixel pixel) {
		mAffectedPixels.add(pixel);
	}
	
	public Pixel getLastPixel() {
		if (!mAffectedPixels.isEmpty()) {
			return mAffectedPixels.get(mAffectedPixels.size() - 1);
		}
		else {
			return null;
		}
	}
	
	public void recycle() {
		mAffectedPixels.clear();
		mAffectedPixels = null;
	}
	
	public void setApplied(boolean applied) {
		mApplied = applied;
	}
	
	public boolean getApplied() {
		return mApplied;
	}
	
	public void doEvent(Bitmap bitmap) {
		for (Pixel pixel : mAffectedPixels) {
			bitmap.setPixel(pixel.mX, pixel.mY, pixel.mNColor);
		}
		mApplied = true;
	}
	
	public void undoEvent(Bitmap bitmap) {
		for (Pixel pixel : mAffectedPixels) {
			bitmap.setPixel(pixel.mX, pixel.mY, pixel.mOColor);
		}
		mApplied = false;
	}
	
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		if (mAffectedPixels != null) {
			mAffectedPixels.clear();
			mAffectedPixels = null;
		}
	}
}
